package com.lxd.milktea.controller;

import com.lxd.milktea.VO.MaterialVO;
import com.lxd.milktea.VO.ResultVO;
import com.lxd.milktea.dataobject.Category;
import com.lxd.milktea.dataobject.Material;
import com.lxd.milktea.dataobject.ProductInfo;
import com.lxd.milktea.form.MaterialForm;
import com.lxd.milktea.form.MaterialRe;
import com.lxd.milktea.form.MaterialReForm;
import com.lxd.milktea.service.MaterialService;
import com.lxd.milktea.util.ResultVOUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ProjectName: milktea
 * @Package: com.lxd.milktea.controller
 * @ClassName: MaterialController
 * @Author: carrymaniac
 * @Description: 原料
 * @Date: 2020/5/12 10:07 上午
 * @Version:
 */
@RestController
@RequestMapping("/material")
public class MaterialController {

    private final MaterialService materialService;

    public MaterialController(MaterialService materialService) {
        this.materialService = materialService;
    }

    /**
     *   返回原料列表 页面用
     *   todo test
     */
    @GetMapping("/list")
    @ApiOperation(value = "页面用 原料列表")
    public ResultVO list(@RequestParam(value = "page",defaultValue = "1")Integer page, @RequestParam(value = "size",defaultValue = "10")Integer size, Map<String,Object> map){
        PageRequest pageRequest = PageRequest.of(page-1,size);
        Page<Material> list = materialService.findList(pageRequest);
        map.put("MaterialPage",list);
        map.put("currentPage",page);
        map.put("pageSize",size);
        return ResultVOUtil.success(map);
    }

    /**
     *  查看原料信息
     *  待测试
     */
    @GetMapping("index")
    @ApiOperation(value = "查看原料的信息")
    public ResultVO index(@RequestParam(value = "materialId",required = false)String materialId,Map<String,Object> map){
        if(!StringUtils.isEmpty(materialId)){
            Material material = materialService.findOne(materialId);
            map.put("material",material);
        }
        Map<String, Integer> son = materialService.findOneSon(materialId);
        ArrayList<String> ids = new ArrayList<>(son.keySet());
        List<Material> list = materialService.findList(ids);
        List<MaterialVO> materialVOList = list.stream().map(material -> {
            Integer needNum = son.get(material.getId());
            MaterialVO vo = new MaterialVO();
            BeanUtils.copyProperties(material, vo);
            vo.setNeedNum(needNum);
            return vo;
        }).collect(Collectors.toList());
        map.put("sonList",materialVOList);
        return ResultVOUtil.success(map);
    }




    /**
     * 新增原料 更新原料信息
     * todo 测试
     * @param form
     * @return
     */
    @PostMapping("save")
    @ApiOperation(value = "保存/更新原料信息")
    public ResultVO save(@ModelAttribute @RequestBody MaterialForm form){
        Material material;
        if (form.getId()==null){
            //为空 是新增
            material = new Material();
            material.setStock(0);
        }else {
            material = materialService.findOne(form.getId());
        }
        material.setMaterialName(form.getMaterialName());
        material.setPic(form.getPic());
        materialService.save(material);
        return ResultVOUtil.success();
    }

    /**
     * 更新原料配方
     * todo test
     */
    @PostMapping("/saveSon")
    @ApiOperation(value = "更新原料配方")
    public ResultVO saveSon(@RequestBody MaterialReForm form){
        String materialId = form.getMaterialId();
        //检查传进来的MaterialId的合法性
        List<String> ids = form.getList().stream().map(MaterialRe::getMaterialId).collect(Collectors.toList());
        List<Material> list = materialService.findList(ids);
        if(ids.size() != list.size()){
            //参数不合法
            return ResultVOUtil.fail(400,"子原料不存在");
        }
        //先删除原来的Re
        materialService.deleteMaterialRe(materialId);
        materialService.saveMaterialRe(materialId,form.getList());
        return ResultVOUtil.success();
    }

    /**
     * 返回原料列表 产品页用
     * todo 测试
      */
    @GetMapping("/listForSelect")
    @ApiOperation(value = "返回原料列表 产品页用")
    public ResultVO list(){
        List<Material> list = materialService.findAll();
        Map<String, String> MaterialList = list.stream().collect(Collectors.toMap(Material::getId, Material::getMaterialName));
        return ResultVOUtil.success(MaterialList);
    }

}
